#include "conversationwindow.h"
#include "ui_conversationwindow.h"

ConversationWindow::ConversationWindow(GenericContact *realparent, ConversationConector *connector, QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui::ConversationWindow)
{
    m_ui->setupUi(this);
    this->realparent = realparent;
    this->timer = new QTimer(this);
    this->connector = connector;
    connect(timer, SIGNAL(timeout()), this, SLOT(dataReception()));
    timer->start(100);
    this->setWindowTitle((QString(tr("Chat with:")).append(QString(" "))).append(realparent->getNick()));
    m_ui->conversationHistory->setAcceptDrops(false);
}

ConversationWindow::~ConversationWindow()
{
    delete m_ui;
}

void ConversationWindow::dataReception()
{
    Packet *paquete;
    this->connector->dataReception();
    while((paquete = this->connector->getPacket()) != NULL)
    {
        if (paquete->isMessagePacket())
        {
            QString temp;
            temp.append("<b>");
            temp.append(Qt::escape(((MessagePacket *)paquete)->getNick()));
            temp.append("&gt;</b> ");
            temp.append(Qt::escape(((MessagePacket *)paquete)->getMessage()));
            m_ui->conversationHistory->append(temp);
        }
    }
}

void ConversationWindow::changeEvent(QEvent *e)
{
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void ConversationWindow::updated(ConversationConector *connector)
{
    this->connector->CloseConnection();
    delete this->connector;
    this->connector = connector;
    if (this->connector->isActive()) {
        QString temp;
        temp.append("<b>*** ");
        temp.append(Qt::escape(tr("Connection reestablished")));
        temp.append(" ***</b>");
        m_ui->conversationHistory->append(temp);
    }
}

void ConversationWindow::closeEvent(QCloseEvent *event)
{
    this->timer->stop();
    delete this->timer;
    delete this->connector;
    this->realparent->conversationEnded();
    event->accept();
}

void ConversationWindow::on_sendButton_clicked()
{
    SettingManager *ultimo = SettingManager::getSettingManager();
    QString alias(ultimo->alias);
    MessagePacket message;
    message.setNick(&alias);
    QString msg(m_ui->messageEdit->text());
    if (!this->connector->isActive())
    {
        if (this->realparent->restartConversation())
        {
            QString temp;
            temp.append("<b>*** ");
            temp.append(Qt::escape(tr("Can't chat in invisible mode")));
            temp.append(" ***</b>");
            m_ui->conversationHistory->append(temp);
            return;
        }
    }
    message.setMessage(&msg);
    if (this->connector->sendPacket(&message))
    {
        QString temp;
        temp.append("<b>*** ");
        temp.append(Qt::escape(tr("Error sending the message")));
        temp.append(" ***</b>");
        m_ui->conversationHistory->append(temp);
    } else {
        QString temp;
        temp.append("<b>&gt; </b> ");
        temp.append(Qt::escape(msg));
        m_ui->conversationHistory->append(temp);
        m_ui->messageEdit->setText(QString());
    }
}


